﻿/*
 *Copyright(C) 2020 by  GYYX All rights reserved.
 *Unity版本：2018.4.23f1 
 *作者:程一峰  
 *创建日期: 2021-04-08 
 *模块说明：
 *版本: 1.2
*/


using UnityEditor;
using UnityEngine;
using Warfare.FSM;

namespace WarfareEditor.FSM
{
    /// <summary>
    /// 状态机编辑器工具；
    /// </summary>
    public static class FSMEditorTool
    {

        private static GUIStyle _NodeStyle;
        private static GUIStyle NodeStyle
        {
            get
            {
                if (_NodeStyle == null)
                {
                    _NodeStyle = new GUIStyle();
                    _NodeStyle.normal.textColor = Color.red;
                    _NodeStyle.alignment = TextAnchor.MiddleCenter;
                    _NodeStyle.fontStyle = FontStyle.Bold;
                }
                return _NodeStyle;
            }
        }

        /// <summary>
        /// 绘制节点；
        /// </summary>
        /// <param name="node"></param>
        public static void DrawNode(FSMNode node)
        {
            EditorGUILayout.BeginVertical(GUILayout.Height(45));
            EditorGUILayout.LabelField(node.IsTwist ? "已取反" : "", NodeStyle);
            EditorGUILayout.EndVertical();
        }

        /// <summary>
        /// 寻找当前位置对应的节点；
        /// </summary>
        /// <param name="window"></param>
        public static FSMNode FindMouseNode(FSMEditorWindow window, Vector2 screenPos)
        {

            //根节点：
            var rootNode = window.CurrentAsset.RootNode;
            if (rootNode.NodeRect.Contains(screenPos))
                return rootNode;

            //其他节点；
            var L = window.CurrentAsset.ListNodes;
            int length = L.Count;
            for (int i = 0; i < length; i++)
            {
                var node = L[i];
                if (node.NodeRect.Contains(screenPos))
                    return node;
            }
            return null;
        }

        /// <summary>
        /// 划线；
        /// </summary>
        /// <param name="startPos">起点</param>
        /// <param name="endPos">终点</param>
        public static void DrawLine(Vector2 startPos, Vector2 endPos, Color col)
        {
            Handles.color = Color.black;
            DrawArrow(startPos, endPos, col);
            Handles.color = Color.white;
        }

        private static void DrawArrow(Vector2 from, Vector2 to, Color color)
        {
            Handles.BeginGUI();
            Handles.color = color;
            Handles.DrawAAPolyLine(3, from, to);
            Vector2 v0 = from - to;
            v0 *= 10 / v0.magnitude;
            Vector2 v1 = new Vector2((v0.x * 0.866f) - (v0.y * 0.5f), (v0.x * 0.5f) + (v0.y * 0.866f));
            Vector2 v2 = new Vector2((v0.x * 0.866f) + (v0.y * 0.5f), (v0.x * -0.5f) + (v0.y * 0.866f));
            Vector2 middle = (from + to) * 0.5f;
            Handles.DrawAAPolyLine(3, middle + v1, middle, middle + v2);
            Handles.EndGUI();
        }

        /// <summary>
        /// 成功后节点起点；
        /// </summary>
        public static Vector2 SucessPoint(this FSMNode node)
        {
            if (node == null)
                return Vector2.zero;

            Vector2 center = node.NodeRect.center;
            center.x = node.NodeRect.xMin + 5;
            center.y = node.NodeRect.yMax - 5;
            return center;
        }

        /// <summary>
        /// 失败后节点起点；
        /// </summary>
        public static Vector2 FailedPoint(this FSMNode node)
        {
            if (node == null)
                return Vector2.zero;

            Vector2 center = node.NodeRect.center;
            center.x = node.NodeRect.xMax - 5;
            center.y = node.NodeRect.yMax - 5;
            return center;
        }

        /// <summary>
        /// 被连接的中间的点；
        /// </summary>
        public static Vector2 TopCenterPoint(this FSMNode node)
        {
            if (node == null)
                return Vector2.zero;

            Vector2 center = node.NodeRect.center;
            center.y = node.NodeRect.yMin + 5;
            return center;
        }

        public static bool IsHasSucessNode(this FSMNode node) => node.SucessNodeIndex > 0;
        public static bool IsHasFailedNode(this FSMNode node) => node.FailedNodeIndex > 0;

        public static void SetSucessNode(this FSMNode node, FSMNode nextNode)
        {
            if (node == null)
                return;

            var next = nextNode != null ? nextNode.Index : -1;
            node.SucessNodeIndex = next;
        }

        public static void SetFailedNode(this FSMNode node, FSMNode nextNode)
        {
            if (node == null)
                return;

            var next = nextNode != null ? nextNode.Index : -1;
            node.FailedNodeIndex = next;
        }

        public static FSMNode GetSucessNode(this FSMNode node, FSMAsset asset) => asset.GetNode(node.SucessNodeIndex);

        public static FSMNode GetFailedNode(this FSMNode node, FSMAsset asset) => asset.GetNode(node.FailedNodeIndex);

        public static FSMNode GetNode(this FSMAsset asset, int index) => asset?.GetNodeByIndex(index);

    }
}
